/*
 * FTNANDC024 driver developed by Mychaela Falconia at CarrierComm, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <linux/io.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/mtd/mtd.h>
#include <linux/jiffies.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/errno.h>

#include "ftnandc024_priv.h"
#include "ftnandc024_regs.h"

irqreturn_t ftnandc024_irq_handler(int irq, void *dev_id)
{
	struct ftnandc024_data *cntr = dev_id;
	uint32_t status;

	status = readl(cntr->io_base + INTR_STATUS);
	if (!status)
		return IRQ_NONE;

	writel(status, cntr->io_base + INTR_STATUS);
	cntr->intr_status |= status;

	wake_up(&cntr->wq);
	return IRQ_HANDLED;
}

static int wait_cmd_complete_poll(struct ftnandc024_data *cntr)
{
	unsigned long timeo = jiffies + HZ * 5;
	uint32_t status = 0;

	while (time_before(jiffies, timeo)) {
		status = readl(cntr->io_base + INTR_STATUS);
		if (status & STATUS_CMD_COMPLETE(0))
			break;
		cond_resched();
	}
	if (!(status & STATUS_CMD_COMPLETE(0))) {
		/* our wait timed out */
		writel(CMDQUEUE_FLUSH_BIT(0), cntr->io_base + CMDQUEUE_FLUSH);
		cntr->reset_needed = true;
		return -EIO;
	} else if (status & STATUS_FAIL(0)) {
		/* erase/program failed */
		cntr->reset_needed = true;
		return -EIO;
	} else	/* all good */
		return 0;
}

static int wait_cmd_complete_irq(struct ftnandc024_data *cntr)
{
	wait_event_timeout(cntr->wq,
			   cntr->intr_status & STATUS_CMD_COMPLETE(0),
			   HZ * 5);
	if (!(cntr->intr_status & STATUS_CMD_COMPLETE(0))) {
		/* our wait timed out */
		writel(CMDQUEUE_FLUSH_BIT(0), cntr->io_base + CMDQUEUE_FLUSH);
		cntr->reset_needed = true;
		return -EIO;
	} else if (cntr->intr_status & STATUS_FAIL(0)) {
		/* erase/program failed */
		cntr->reset_needed = true;
		return -EIO;
	} else	/* all good */
		return 0;
}

int ftnandc024_execute_command(struct ftnandc024_data *cntr, uint32_t command)
{
	/* clear controller and software state before issuing command */
	writel(0xFFFFFFFF, cntr->io_base + INTR_STATUS);
	cntr->intr_status = 0;

	/* issue command */
	writel(command | CMD_COMPLETE_EN, cntr->io_base + CMDQUEUE4(0));

	/* await completion */
	if (cntr->use_irq)
		return wait_cmd_complete_irq(cntr);
	else
		return wait_cmd_complete_poll(cntr);
}

int ftnandc024_reset_nand_chip(struct ftnandc024_data *cntr)
{
	cntr->reset_needed = false;
	return ftnandc024_execute_command(cntr, CMD_INDEX(FIXFLOW_RESET));
}
